package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.DescribeMediaInfosRequest;
import com.tencentcloudapi.vod.v20180717.models.DescribeMediaInfosResponse;
import com.tencentcloudapi.vod.v20180717.models.MediaInfo;
import com.tencentcloudapi.vod.v20180717.models.MediaMetaData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;
    @Autowired
    private VodUploadClient vodUploadClient;

    @Override
    public Map<String, String> uploadTrack(MultipartFile file) {

        HashMap<String, String> fileInfoMap = new HashMap<>();
        VodUploadRequest request = new VodUploadRequest();

        String tempPath= UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(),file);
        try {
            request.setMediaFilePath(tempPath);
            VodUploadResponse response = vodUploadClient.upload(vodConstantProperties.getRegion(), request);

            if (response != null) {
                fileInfoMap.put("mediaFileId", response.getFileId());
                fileInfoMap.put("mediaUrl", response.getMediaUrl());
            }
            log.info("文件上传成功,文件名称为:" + file.getOriginalFilename());
        } catch (Exception e) {

            log.error("文件上传失败,文件名称为:" + file.getOriginalFilename());
        }

        return fileInfoMap;
    }

    @Override
    public TrackMediaInfoVo getMediaInfo(String mediaFileId) {
        TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();
        try {
            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());

            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());

            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            String[] fileIds = {mediaFileId};
            req.setFileIds(fileIds);

            DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);

            if (resp != null) {
                MediaInfo[] mediaInfoSet = resp.getMediaInfoSet();

                if (mediaInfoSet != null && mediaInfoSet.length > 0) {

                    MediaInfo mediaInfo = mediaInfoSet[0];
                    String mediaUrl = mediaInfo.getBasicInfo().getMediaUrl();
                    String type = mediaInfo.getBasicInfo().getType();
                    MediaMetaData metaData = mediaInfo.getMetaData();
                    Float duration = metaData.getDuration();
                    Long size = metaData.getSize();

                    trackMediaInfoVo.setDuration(duration);
                    trackMediaInfoVo.setSize(size);
                    trackMediaInfoVo.setType(type);
                    trackMediaInfoVo.setMediakUrl(mediaUrl);
                }
            }


            log.info("获取详细信息成功");
        } catch (TencentCloudSDKException e) {
            log.error("获取详细信息失败");
        }
        return trackMediaInfoVo;
    }
}
